﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SOWY.common;
using System.Windows.Forms;
namespace SOWY
{
    class GUILogic
    {
        private static GUILogic objGUILogicInstance = null;

        private GUILogic()
        {
        }

        public static GUILogic getInstance()
        {
            if (objGUILogicInstance == null)
            {
                objGUILogicInstance = new GUILogic();
            }
            return objGUILogicInstance;
        }

        public bool isIndexInRange(List<Task> tasks, int index)
        {
            return index < tasks.Count || index > 0;
        }

        public string processTask(Task task)
        {

            string line = task.task;

            if ((task.startDate != DateTime.MinValue && task.endDate != DateTime.MinValue))
            {
                line += " from " + task.startDate.ToShortDateString() + " to " + task.endDate.ToShortDateString();
            }
            else if (task.startDate == DateTime.MinValue && task.endDate == DateTime.MinValue)
            {

            }
            else if (task.startDate == DateTime.MinValue)
            {
                line += " on " + task.endDate.ToShortDateString();
            }
            else if (task.endDate == DateTime.MinValue)
            {
                line += " on " + task.startDate.ToShortDateString();
            }
            return line;
        }

        public string getDateLabelText(string startDate, string endDate)
        {
            string text;
            string minValue = DateTime.MinValue.ToShortDateString();
            if (startDate.Equals(minValue) && endDate.Equals(minValue))
            {
                text = GeneralConstant.DESIGN_AGENDA_TO_DO_STRING;
            }
            else if (startDate.Equals(minValue))
            {
                text = endDate;
            }
            else if (endDate.Equals(minValue))
            {
                text = startDate;
            }
            else
            {
                text = startDate + " -- " + endDate;
            }
            return text;
        }

        public bool checkDeleteCommand(string command)
        {
            for (int i = 0; i < GeneralConstant.KEY_POSSIBLE_DELETE.Length; i++)
            {
                if (command == GeneralConstant.KEY_POSSIBLE_DELETE[i])
                {
                    return true;
                }

            }
            return false;
        }

        public bool checkUpdateCommand(string command)
        {
            for (int i = 0; i < GeneralConstant.KEY_POSSIBLE_UPDATE.Length; i++)
            {
                if (command == GeneralConstant.KEY_POSSIBLE_UPDATE[i])
                {
                    return true;
                }

            }
            return false;
        }

        public bool checkUndoCommand(string command)
        {
            for (int i = 0; i < GeneralConstant.KEY_POSSIBLE_UNDO.Length; i++)
            {
                if (command == GeneralConstant.KEY_POSSIBLE_UNDO[i])
                {
                    return true;
                }

            }
            return false;
        }

        public bool checkExitCommand(string command)
        {
            for (int i = 0; i < GeneralConstant.KEY_POSSIBLE_EXIT.Length; i++)
            {
                if (command == GeneralConstant.KEY_POSSIBLE_EXIT[i])
                {
                    return true;
                }
            }
            return false;
        }

        public string getErrorMessage(List<Task> list)
        {
            string output = "";
            if (list.Count == 0)
                output = GeneralConstant.INFO_DO_NOT_UNDERSTAND + "\n";
            else
                foreach (Task t in list)
                {
                    output += t.task + "\n";
                }
            return output;
        }

        public string getErrorMessage(List<Command> list)
        {
            string output = "";
            if (list.Count == 0)
                output = GeneralConstant.INFO_DO_NOT_UNDERSTAND + "\n";
            else
                foreach (Command c in list)
                {
                    output += c.taskName + "\n";
                }
            return output;
        }

        public bool isError(List<Task> tasks)
        {
            if (tasks.Count == 0 || tasks[0].type.Equals(GeneralConstant.KEY_ERROR))
                return true;
            else
                return false;
        }

        public bool isError(List<Command> list)
        {
            if (list.Count == 0 || list[0].taskName.Equals(GeneralConstant.KEY_ERROR))
                return true;
            else
                return false;
        }


        public  bool isEmptyList(List<Command> list)
        {
            return list.Count == 0;
        }

        public bool isEmptyList(List<Task> list)
        {
            return list.Count == 0;
        }

        public  bool isEmptyString(string input)
        {

            return (input == null || input.Trim().Length == 0);
        }

        public string getDate(Task task){
            string line = "";
            if ((task.startDate != DateTime.MinValue && task.endDate != DateTime.MinValue))
            {
                //line += " from " + task.startDate.ToShortDateString() + " to " + task.endDate.ToShortDateString();
            }
            else if (task.startDate == DateTime.MinValue && task.endDate == DateTime.MinValue)
            {
                line = DateTime.MinValue.ToShortDateString();
            }
            else if (task.startDate == DateTime.MinValue)
            {
                line += task.endDate.ToShortDateString();
            }
            else if (task.endDate == DateTime.MinValue)
            {
                line +=  task.startDate.ToShortDateString();
            }
            return line;
        }

        public string getDate(DateTime startDate, DateTime endDate)
        {
            string line = "";
            if ((startDate != DateTime.MinValue && endDate != DateTime.MinValue))
            {
                //line += " from " + task.startDate.ToShortDateString() + " to " + task.endDate.ToShortDateString();
            }
            else if (startDate == DateTime.MinValue && endDate == DateTime.MinValue)
            {
                line = DateTime.MinValue.ToShortDateString();
            }
            else if (startDate == DateTime.MinValue)
            {
                line += endDate.ToShortDateString();
            }
            else if (endDate == DateTime.MinValue)
            {
                line += startDate.ToShortDateString();
            }
            return line;
        }

        public void showErrorMessageBox(string errorMessage)
        {
            if (!isEmptyString(errorMessage))
            {
                MessageBox.Show(errorMessage, GeneralConstant.ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
